//-----------------------------------------------------------------------------------
#include "Player.h"
#include "game.h"
//-----------------------------------------------------------------------------------
int Player::m_iGold;
float Player::m_fpX;
float Player::m_fpY;
int Player::m_iTownRow;
int Player::m_iTownCol;
int Player::m_iMineRow;
int Player::m_iMineCol;
AIStar Player::m_kAIStarTown;
AIStar Player::m_kAIStarMine;
sPosition ** Player::m_pkPathToMine;
sPosition ** Player::m_pkPathToTown;
//-----------------------------------------------------------------------------------
Player::Player()
{
	
}
//-----------------------------------------------------------------------------------
Player::~Player()
{

}
//-----------------------------------------------------------------------------------
bool Player::Start(int mapWidth, int mapHeight)
{
	int iGetTile = 0;
	int kTileMatrix[MAP_ROWS][MAP_COLS];

	for( int i = 0; i < mapWidth; i++)
	{
		for( int j = 0; j < mapHeight; j++)
		{
			iGetTile = TestGame::m_kTileMap.GetTileFromColRow(j, i);

			if(iGetTile == PLAYER_TILE)
			{
				m_iTownRow = i;
				m_iTownCol = j;
			}

			if(iGetTile == MINE_TILE)
			{
				m_iMineRow = i;
				m_iMineCol = j;
			}

			if(iGetTile == 3)
			{
				kTileMatrix[i][j] = TYPE_WALL;
			}
			else
			{
				kTileMatrix[i][j] = TYPE_NORM;	
			}
		}
	}
	
	float fpStartPosX = 0;
	float fpStartPosY = 0;

	m_kAIStarTown.InitializeASTar(kTileMatrix, mapWidth - 1, mapHeight - 1);
	m_kAIStarMine.InitializeASTar(kTileMatrix, mapWidth - 1, mapHeight - 1);

	m_pkPathToMine = m_kAIStarTown.FindPath(m_iTownCol, m_iTownRow, m_iMineCol, m_iMineRow);
	m_pkPathToTown = m_kAIStarMine.FindPath(m_iMineCol, m_iMineRow, m_iTownCol, m_iTownRow);

	TestGame::m_kTileMap.SetTile(m_iTownCol, m_iTownRow, MINE_TILE);
	TestGame::m_kTileMap.SetTile(m_iMineCol, m_iMineRow, MINE_TILE);

	TestGame::m_kFSMPlayer->SetEvent(EVENT_TOMINE);

	return true;
}
//-----------------------------------------------------------------------------------
bool Player::WalkToMine()
{
	if(!m_pkPathToMine[TestGame::m_iPos])
	{
		return false;
	}

	if(m_iMineRow == m_pkPathToMine[TestGame::m_iPos]->m_isRow && 
		m_iMineCol == m_pkPathToMine[TestGame::m_iPos]->m_isCol)
	{
		TestGame::m_kFSMPlayer->SetEvent(EVENT_STARTMINING);
	}
	else if(m_iTownRow == m_pkPathToMine[TestGame::m_iPos]->m_isRow && 
		m_iTownCol == m_pkPathToMine[TestGame::m_iPos]->m_isCol)
	{
		TestGame::m_kFSMPlayer->SetEvent(EVENT_UNLOAD);
	}
	else
	{
		TestGame::m_kFSMPlayer->SetEvent(EVENT_TOIDLE);
	}

	TestGame::m_kTileMap.SetTile(m_pkPathToMine[TestGame::m_iPos]->m_isCol,
								 m_pkPathToMine[TestGame::m_iPos]->m_isRow,
								 PLAYER_TILE);
	
	if(TestGame::m_iPos >= 1)
	{
		TestGame::m_kTileMap.SetTile(m_pkPathToMine[TestGame::m_iPos - 1]->m_isCol,
									 m_pkPathToMine[TestGame::m_iPos - 1]->m_isRow,
									 EMPTY_TILE);
	}

	++TestGame::m_iPos;

	if(m_pkPathToMine[TestGame::m_iPos] == NULL)
	{
		TestGame::m_kTileMap.SetTile(m_iTownCol, m_iTownRow, EMPTY_TILE);
		TestGame::m_kTileMap.SetTile(m_iMineCol, m_iMineRow, MINE_TILE);

		return true;
	}

	return true;
}
//-----------------------------------------------------------------------------------
bool Player::ReturnFromMine()
{
	if(!m_pkPathToTown[TestGame::m_iPos])
	{
		return false;
	}

	if(m_iMineRow == m_pkPathToTown[TestGame::m_iPos]->m_isRow && 
		m_iMineCol == m_pkPathToTown[TestGame::m_iPos]->m_isCol)
	{
		TestGame::m_kFSMPlayer->SetEvent(EVENT_STARTMINING);
	}
	else if(m_iTownRow == m_pkPathToTown[TestGame::m_iPos]->m_isRow && 
		m_iTownCol == m_pkPathToTown[TestGame::m_iPos]->m_isCol)
	{
		TestGame::m_kFSMPlayer->SetEvent(EVENT_UNLOAD);
	}
	else
	{
		TestGame::m_kFSMPlayer->SetEvent(EVENT_TOIDLE);
	}

	TestGame::m_kTileMap.SetTile(m_pkPathToTown[TestGame::m_iPos]->m_isCol,
								 m_pkPathToTown[TestGame::m_iPos]->m_isRow,
								 PLAYER_TILE);

	if(TestGame::m_iPos >= 1)
	{
		TestGame::m_kTileMap.SetTile(m_pkPathToTown[TestGame::m_iPos - 1]->m_isCol,
									 m_pkPathToTown[TestGame::m_iPos - 1]->m_isRow,
									 EMPTY_TILE);

		TestGame::m_kTileMap.SetTile(m_iMineCol,m_iMineRow, MINE_TILE);
	}

	++TestGame::m_iPos;

	if(m_pkPathToTown[TestGame::m_iPos] == NULL)
	{
		TestGame::m_kTileMap.SetTile(m_iTownCol, m_iTownRow, EMPTY_TILE);
		TestGame::m_kTileMap.SetTile(m_iMineCol, m_iMineRow, MINE_TILE);

		return true;
	}

	return true;
}
//-----------------------------------------------------------------------------------
bool Player::Mining()
{
	TestGame::m_iPos = 0;

	if(m_iGold <= MAX_GOLD)
	{
		m_iGold += 5;
		TestGame::m_iMineGold -= 5;
	}
	else
	{
		TestGame::m_kFSMPlayer->SetEvent(EVENT_TOTOWN);
	}
	
	return true;
}
//-----------------------------------------------------------------------------------
bool Player::Dump()
{
	TestGame::m_iPos = 0;

	TestGame::m_iTownGold += m_iGold;
	
	m_iGold = 0;

	if(TestGame::m_iMineGold == 0)
	{
		TestGame::m_kFSMPlayer->SetEvent(EVENT_TOIDLE);
	}
	else
	{
		TestGame::m_kFSMPlayer->SetEvent(EVENT_TOMINE);
	}

	return true;
}
//-----------------------------------------------------------------------------------